home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-09-28 | 7.7 KB | 204 lines | [TEXT/CWIE] |
- /*
- File: Radio.java
-
- Copyright: © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
-
- Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
- ("Apple") in consideration of your agreement to the following terms, and your
- use, installation, modification or redistribution of this Apple software
- constitutes acceptance of these terms. If you do not agree with these terms,
- please do not use, install, modify or redistribute this Apple software.
-
- In consideration of your agreement to abide by the following terms, and subject
- to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
- copyrights in this original Apple software (the "Apple Software"), to use,
- reproduce, modify and redistribute the Apple Software, with or without
- modifications, in source and/or binary forms; provided that if you redistribute
- the Apple Software in its entirety and without modifications, you must retain
- this notice and the following text and disclaimers in all such redistributions of
- the Apple Software. Neither the name, trademarks, service marks or logos of
- Apple Computer, Inc. may be used to endorse or promote products derived from the
- Apple Software without specific prior written permission from Apple. Except as
- expressly stated in this notice, no other rights or licenses, express or implied,
- are granted by Apple herein, including but not limited to any patent rights that
- may be infringed by your derivative works or by other works in which the Apple
- Software may be incorporated.
-
- The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
- WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
- WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
- COMBINATION WITH YOUR PRODUCTS.
-
- IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
- OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
- (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- Change History (most recent first):
-
- */
-
-
- package com.apple.jens.radio;
-
- import java.io.*;
- import java.net.*;
- import java.util.Date;
-
- import com.apple.mrj.*;
-
-
- /** This is the main class of the streaming MP3 server.
- It contains the "main" method that launches the server.
- It also has static functions for console-based logging,
- and stores the global default property values.
-
- */
-
- public class Radio {
-
- public static final String kStationsFolderName = "Stations"; //FIX: Needs to be localizable
-
- public static final String kPropBufferSize = "buffer-size",
- kPropLogLevel = "log-level";
-
-
- /** The "main" method that launches the server.
- Command-line arguments are currently ignored.
- Default property values are read from the "Defaults" file
- in the current directory,
- and station settings are read from files in the "Stations" directory. */
- public static void main( String[] args ) {
- System.out.println("*** "+getNameAndVersion()+" *** Please excuse the cheesy console based UI...");
- try{
- RadioProperties.sDefaults = new RadioProperties(new File("Defaults"),null);
- }catch( IOException x ) {
- WARN(1,"Radio","Couldn't read default settings from 'Defaults' file:");
- if( sLogLevel>=1 )
- x.printStackTrace(System.err);
- return;
- }
-
- sLogLevel = RadioProperties.sDefaults.getIntProperty(kPropLogLevel,sLogLevel);
-
- // Get buffer size from defaults:
- int size = RadioProperties.sDefaults.getIntProperty(kPropBufferSize,0);
- if( size >= 8000 )
- Buffer.sSize = size;
-
- // Create a Station for each valid station file in the Stations directory:
- File stationsDir = new File(kStationsFolderName);
- if( !stationsDir.exists() || !stationsDir.isDirectory() ) {
- WARN(1,"Radio","Couldn't find "+kStationsFolderName+" folder");
- return;
- }
- String[] stationFiles = stationsDir.list();
-
- for( int i=0; i<stationFiles.length; i++ ) {
- File stationFile = new File(stationsDir,stationFiles[i]);
- if( isValidStationFile(stationFile) ) {
- LOG(2,"Radio", "Reading station file "+stationFile);
- try{
- RadioProperties stationProps = new RadioProperties(stationFile,RadioProperties.sDefaults);
- new Station(stationProps); // Creates & starts a new Station thread
- }catch( IOException x ) {
- WARN(1,"Radio","Couldn't initialize station:");
- if( sLogLevel>=1 )
- x.printStackTrace(System.err);
- }
- } else {
- WARN(2,"Radio", "Skipping \""+stationFile+"\" which isn't a valid station file"
- +" (it's a directory, or invisible, or filetype isn't TEXT)");
- }
- }
- }
-
-
- /** Determines whether a file in the Stations directory is a valid station file.
- Files whose names start with "." are skipped,
- and on Mac OS so are files whose file-type is not 'TEXT'. */
- private static boolean isValidStationFile( File f ) {
- try{
- if( f.isDirectory() )
- return false;
- else if( f.getName().startsWith(".") ) // Really only applies to Unix...
- return false;
- else if( MRJApplicationUtils.isMRJToolkitAvailable() &&
- ! MRJFileUtils.getFileType(f).equals(MRJOSType.kTypeTEXT) )
- return false;
- else
- return true;
- //! In Java2 should also check File.isHidden
- }catch( IOException x ) {
- x.printStackTrace();
- return false;
- }
- }
-
-
- // LOGGING / WARNINGS:
-
-
- /** The global logging level; controls verbosity of console output.
- Initialized from the kPropLogLevel property on startup.
- <p>0: No output
- <br>1: "Normal" output, mostly just track and connection info and important warnings.
- <br>2: Adds information about buffers being filled and transmitted.
- <br>3: Detailed synchronization info, for debugging purposes only. */
- public static int sLogLevel = 1;
-
-
- /** Returns the name and version number of the server software. */
- public static String getNameAndVersion( ) {
- return "Radio 0.3"; //FIX: Use real name
- }
-
-
- /** Writes a message to the log (currently the console at System.out.)
- @param level The level of message; sLogLevel has to be at least
- this high to print the message.
- @param who The object generating this message
- @param message The message to display. */
- public static void LOG( int level, Object who, String message ) {
- if( sLogLevel >= level ) {
- timestamp(System.out, level, who+": "+message);
- System.out.flush();
- }
- }
-
-
- /** Writes a warning to the log (currently the console at System.err.)
- @param level The level of warning; sLogLevel has to be at least
- this high to print the message.
- @param who The object generating this warning
- @param message The warning message to display. */
- public static void WARN( int level, Object who, String message ) {
- if( sLogLevel >= level )
- timestamp(System.err, level, "WARNING: "+who+": "+message);
- }
-
-
- /** Writes a time-stamp if it's been long enough since the last one was written. */
- private static void timestamp( PrintStream s, int level, String msg ) {
- long now = System.currentTimeMillis();
- if( now - sLastStamp > kStampInterval ) {
- s.println("____" + (new Date(now)) + "____");
- sLastStamp = now;
- }
- s.print(level+"| ");
- s.println(msg);
- }
-
-
- /** The time at which the last timestamp was written. */
- private static long sLastStamp = 0;
-
- /** How often (in millis) timestamps are written to the log */
- private static final long kStampInterval = 1 * 60 * 1000; // 1 minute
-
- }
-